home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_05 / colton / xwd.c < prev    next >
C/C++ Source or Header  |  1995-02-21  |  2KB  |  84 lines

  1. /*
  2.  * Listing 6 - xwd.c (loader)
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <math.h>
  7. #include <X11/XWDFile.h>
  8. #include "ips_image.h"
  9.  
  10. void        read_ximage();
  11.  
  12. int
  13. main(argc, argv)
  14. int argc;
  15. char **argv;
  16. {
  17.     ips_header  header;
  18.     ips_image   image;
  19.  
  20.     
  21.     if (argc < 3) {
  22.         printf("Usage: %s infile outfile\n", argv[0]);
  23.         exit(1);
  24.     }
  25.     
  26.     read_ximage(&header, &image, argv[1]);
  27.  
  28.     img_to_ips(&header, &image, argv[2]);
  29.  
  30.     exit(0);
  31. }
  32.  
  33.  
  34. void 
  35. read_ximage(header, image, filename)
  36. ips_header    *header;
  37. ips_image    *image;
  38. char        *filename;
  39. {
  40.     int                i, pad, rightpad;
  41.     char            buf[1024];
  42.     FILE            *fp;
  43.     unsigned char    *p;
  44.     XWDColor         xcmap[256];
  45.     XWDFileHeader     xheader;
  46.  
  47.  
  48.     if((fp = fopen(filename, "r")) == NULL) {
  49.         perror("read_ximage");
  50.         exit(1);
  51.     }
  52.  
  53.     fread(&xheader, 1, sizeof(XWDFileHeader), fp);
  54.     fread(buf, 1, (xheader.header_size - sizeof(XWDFileHeader)), fp);
  55.     fread(xcmap, 1, sizeof(XWDColor) * xheader.ncolors, fp);
  56.  
  57.     p = image->data = (unsigned char *)
  58.             malloc(xheader.pixmap_width * xheader.pixmap_height);
  59.  
  60.     rightpad = xheader.bytes_per_line - xheader.pixmap_width;
  61.  
  62.     for (i=0; i<xheader.pixmap_height; i++) {
  63.         fread((p + (i * xheader.pixmap_width)), 1, 
  64.             xheader.pixmap_width, fp);
  65.         
  66.         for (pad=0; pad < rightpad; pad++) 
  67.             fgetc(fp); 
  68.     }
  69.  
  70.     header->bpp            = 8;
  71.     header->cmap_count    = xheader.ncolors;
  72.     header->width        = xheader.pixmap_width;
  73.     header->height        = xheader.pixmap_height;
  74.  
  75.     for(i=0; i < header->cmap_count; i++) {
  76.         image->cmap[i].pixel = i;
  77.         image->cmap[i].red     = xcmap[i].red >> 8;
  78.         image->cmap[i].green = xcmap[i].green >> 8;
  79.         image->cmap[i].blue     = xcmap[i].blue >> 8;
  80.     }
  81.  
  82.     fclose(fp);
  83. }
  84.